﻿package MemoryDB;

import java.io.Serializable;

import Public.RefObject;

public class YuanTable implements Serializable
{
	//public YuanTable(string name)
	//{
	//    rows = new List<YuanRow>();
	//    deleteRows = new List<YuanRow>();
	//    listSql = new List<string>();
	//    tableName = name;
	//    ip = "";
	//    port = 0;
	//    tableKey = "";
	//}

	public YuanTable(String name, String mTableKey)
	{
		rows = new java.util.ArrayList<YuanRow>();
		deleteRows = new java.util.ArrayList<YuanRow>();
		listSql = new java.util.ArrayList<String>();
		tableName = name;
		ip = "";
		port = 0;
		tableKey = mTableKey;
		isUpdate = false;
	}

	public YuanTable(String name, String mTableKey, String mIp, int mPort)
	{
		rows = new java.util.ArrayList<YuanRow>();
		deleteRows = new java.util.ArrayList<YuanRow>();
		listSql = new java.util.ArrayList<String>();
		tableName = name;
		ip = mIp;
		port = mPort;
		tableKey = mTableKey;
		isUpdate = false;
	}

	private java.util.ArrayList<String> listSql;


	private boolean isUpdate;
	/** 
	 表是否在更新
	 
	*/
	public final boolean getIsUpdate()
	{
		return isUpdate;
	}
	public final void setIsUpdate(boolean value)
	{
		isUpdate = value;
	}

	/** 
	 表中行数
	 
	*/
	public final int getCount()
	{
		return rows.size();
	}

	private String tableKey;
	/** 
	 表中id
	 
	*/
	public final String getTableKey()
	{
		return tableKey;
	}
	public final void setTableKey(String value)
	{
		tableKey = value;
	}

	private String ip;
	/** 
	 数据库端ip
	 
	*/
	public final String getIp()
	{
		return ip;
	}
	public final void setIp(String value)
	{
		ip = value;
	}


	private int port;
	/** 
	 端口
	 
	*/
	public final int getPort()
	{
		return port;
	}
	public final void setPort(int value)
	{
		port = value;
	}


	private String tableName;
	/** 
	 获取或设置表表的名称
	 
	 <value>
	 The name of the table.
	 </value>
	*/
	public final String getTableName()
	{
		return this.tableName;
	}
	public final void setTableName(String value)
	{
		tableName = value;
	}


	private java.util.ArrayList<YuanRow> rows;

	/** 
	 获取或设置表中的行
	 
	 <value>
	 The rows.
	 </value>
	*/
	public final java.util.ArrayList<YuanRow> getRows()
	{
		return this.rows;

	}
	public final void setRows(java.util.ArrayList<YuanRow> value)
	{
		rows = value;
	}

	private java.util.ArrayList<YuanRow> deleteRows;
	/** 
	 获取或设置表中已删除的行
	 
	 <value>
	 The delete rows.
	 </value>
	*/
	public final java.util.ArrayList<YuanRow> getDeleteRows()
	{
		return this.deleteRows;
	}
	public final void setDeleteRows(java.util.ArrayList<YuanRow> value)
	{
		deleteRows = value;
	}

	/** 
	 从索引获取或设置表中的行 <see cref="yuan.YuanMemoryDB.YuanTable"/>
	 
	 <param name='index'>
	 Index.
	 
	*/
	public final YuanRow getItem(int index)
	{
		if (index > rows.size())
		{

		}
		else
		{
			if (rows.size() > 0)
			{
				return this.rows.get(index);
			}
			else
			{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//#if DEBUG
					//Debug.LogError("超出row的最大索引值");
//#endif
			}
		}
		return null;
	}
	public final void setItem(int index, YuanRow value)
	{
		if (index > rows.size())
		{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//#if DEBUG
				//Debug.LogError("超出row的最大索引值");
//#endif
		}
		else
		{
			if (rows.size() > 0)
			{
				this.rows.set(index, value);
			}
			else
			{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//#if DEBUG
					//Debug.LogError("超出row的最大索引值");
//#endif
			}
		}
	}

	/** 
	  从YuanRow获取或设置表中的行<see cref="yuan.YuanMemoryDB.YuanTable"/>
	 
	 <param name='row'>
	 Row.
	 
	 <exception cref='System.Exception'>
	 Represents errors that occur during application execution.
	 
	*/
	public final YuanRow getItem(YuanRow row)
	{
		for (YuanRow r : rows)
		{
			if (r == row)
			{
				return r;
			}
		}
		return null;

	}
	public final void setItem(YuanRow row, YuanRow value)
	{
		boolean has = false;
		for (YuanRow r : rows)
		{
			if (r == row)
			{
				row = value;
				has = true;
			}
		}
		if (!has)
		{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//#if DEBUG
				//Debug.LogError("找不到指定的YuanRow");
//#endif
			throw new RuntimeException("找不到指定的YuanRow");
		}
	}




	/** 
	 添加行
	 
	 <param name='row'>
	 YuanRow.
	 
	*/
	public final void Add(YuanRow row)
	{
		row.setRowState(YuanRowState.Insert);
		rows.add(row);
	}

	/** 
	 根据索引删除行
	 
	 <param name='index'>
	 If set to <c>true</c> index.
	 
	*/
	public final boolean Delete(int index)
	{
		if (rows.size() > 0 && rows.size() <= index)
		{

			rows.get(index).setRowState(YuanRowState.Delete);
			rows.remove(index);
			deleteRows.add(rows.get(index));
			return true;
		}
		else
		{
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
//#if DEBUG
			//Debug.LogError("超出row的最大索引值");
//#endif
			return false;

		}
	}

	/** 
	 根据YuanRow删除行
	 
	 <param name='row'>
	 If set to <c>true</c> row.
	 
	*/
	public final boolean Delete(YuanRow row)
	{
		
			this.getItem(row).setRowState(YuanRowState.Delete);
			deleteRows.add(row);
			return rows.remove(row);
			
	}

	/** 
	 清空表
	 
	*/
	public final void Clear()
	{
		//foreach (YuanRow r in rows)
		//{
		//    r.RowState = YuanRowState.Delete;
		//    deleteRows.Add(r);
		//}
		rows.clear();
	}

	/** 
	 刷新表中的所有行的标记为Noting
	 
	*/
	public final void Refresh()
	{
		for (YuanRow r : this.rows)
		{
			r.setRowState(YuanRowState.Noting);
			for (java.util.Map.Entry<String, YuanColumn> c : r.getRowItem().entrySet())
			{
				c.getValue().setYuanColumnState(YuanRowState.Noting);
			}
		}
		for (YuanRow r : this.deleteRows)
		{
			r.setRowState(YuanRowState.Noting);
		}
	}

	/** 
	 根据字符串查询表,返回第一个匹配项
	 
	 <param name='strName'>
	 要搜索的字段名
	 
	 <param name='strValue'>
	 要搜索的字段值
	 
	*/
	public final YuanRow SelectRow(String strName, String strValue)
	{
		//YuanRow returnRow = new YuanRow();
		String temp;
		for (YuanRow row : rows)
		{
			temp = row.getItem(strName).getYuanColumnText();
			if (temp.indexOf(strValue) != -1)
			{

				return row;
			}
		}
		return null;
	}

	/** 
	 根据字符串查询表,返回第一个匹配项
	 
	 <param name='strName'>
	 要搜索的字段名
	 
	 <param name='strValue'>
	 要搜索的字段值
	 
	*/
	public final YuanRow SelectRowEqual(String strName, String strValue)
	{
		//YuanRow returnRow = new YuanRow();
		String temp;
		for (YuanRow row : rows)
		{
			temp = row.getItem(strName).getYuanColumnText();
			if (strValue.equals(temp))
			{

				return row;
			}
		}
		return null;
	}

	/** 
	 根据字符串查询表
	 
	 <param name='strName'>
	 要搜索的字段名
	 
	 <param name='strValue'>
	 要搜索的字段值
	 
	*/
	public final YuanRow[] SelectRows(String strName, String strValue)
	{
		java.util.ArrayList<YuanRow> listRetrueRows = new java.util.ArrayList<YuanRow>();
		String temp;
		for (YuanRow row : rows)
		{
			temp = row.getItem(strName).getYuanColumnText();
			if (temp.indexOf(strValue) != -1)
			{
				listRetrueRows.add(row);
			}
		}
		if (listRetrueRows.size() > 0)
		{
			YuanRow[] tempRetrue = new YuanRow[listRetrueRows.size()];
			return listRetrueRows.toArray(tempRetrue);
		}
		else
		{
			return null;
		}
	}

	/** 
	 根据字符串查询表
	 
	 <param name='strName'>
	 要搜索的字段名
	 
	 <param name='strValue'>
	 要搜索的字段值
	 
	*/
	public final YuanRow[] SelectRowsEqual(String strName, String strValue)
	{
		java.util.ArrayList<YuanRow> listRetrueRows = new java.util.ArrayList<YuanRow>();
		String temp;
		for (YuanRow row : rows)
		{
			temp = row.getItem(strName).getYuanColumnText();
			if (strValue.equals(temp))
			{
				listRetrueRows.add(row);
			}
		}
		if (listRetrueRows.size() > 0)
		{
			YuanRow[] tempRetrue = new YuanRow[listRetrueRows.size()];
			return listRetrueRows.toArray(tempRetrue);
		}
		else
		{
			return null;
		}
	}

	/** 
	 根据字符串查询表
	 
	 <param name='strName'>
	 要搜索的字段名
	 
	 <param name='strValue'>
	 要搜索的字段值
	 
	*/
	public final java.util.ArrayList<YuanRow> SelectRowsList(String strName, String strValue)
	{
		java.util.ArrayList<YuanRow> listRetrueRows = new java.util.ArrayList<YuanRow>();
		String temp;
		for (YuanRow row : rows)
		{
			temp = row.getItem(strName).getYuanColumnText();
			if (temp.indexOf(strValue) != -1)
			{
				listRetrueRows.add(row);
			}
		}
		if (listRetrueRows.size() > 0)
		{
			return listRetrueRows;
		}
		else
		{
			return null;
		}
	}


	/** 
	 根据字符串查询表
	 
	 <param name='strName'>
	 要搜索的字段名
	 
	 <param name='strValue'>
	 要搜索的字段值
	 
	*/
	public final java.util.ArrayList<YuanRow> SelectRowsListEqual(String strName, String strValue)
	{
		java.util.ArrayList<YuanRow> listRetrueRows = new java.util.ArrayList<YuanRow>();
		String temp;
		for (YuanRow row : rows)
		{
			temp = row.getItem(strName).getYuanColumnText();
			if (strValue.equals(temp))
			{
				listRetrueRows.add(row);
			}
		}
		if (listRetrueRows.size() > 0)
		{
			return listRetrueRows;
		}
		else
		{
			return null;
		}
	}

	/** 
	 转换成字典集合
	 
	 @return 
	*/
	public final java.util.HashMap<Short, Object> TableToDictionary()
	{


		java.util.HashMap<Short, Object> retureDic = new java.util.HashMap<Short, Object>();
		int i = 0;
		if (this.getCount() > 0)
		{
			for (java.util.Map.Entry<String, YuanColumn> itemR : this.rows.get(0).getRowItem().entrySet())
			{
				i++;
				String[] listR = new String[this.rows.size() + 1];
				listR[0] = itemR.getKey();
				int num = 1;
				for (YuanRow r : this.getRows())
				{
					listR[num] = r.getItem(itemR.getKey()).getYuanColumnText();
					num++;
				}
				
				
				retureDic.put((short)i, listR);
			}
		}
		java.util.HashMap<String, String> tableInfo = new java.util.HashMap<String, String>();
		tableInfo.put("TableName", this.tableName);
		tableInfo.put("TableKey", this.tableKey);
		if (!retureDic.containsKey(0))
		{
			retureDic.put((short)0, tableInfo);
		}
		else
		{
			retureDic.put((short)0, tableInfo);

		}
		return retureDic;
	}

	/** 
	 转换成字典集合并附带一个参数字典（参数字典不可嵌套）
	 
	 @param mRang 参数字典
	 @return 
	*/
	public final java.util.HashMap<Short, Object> TableToDictionary(java.util.HashMap<Short, Object> mRang)
	{


		java.util.HashMap<Short, Object> retureDic = new java.util.HashMap<Short, Object>();
		int i = 1;
		if (this.getCount() > 0)
		{
			for (java.util.Map.Entry<String, YuanColumn> itemR : this.rows.get(0).getRowItem().entrySet())
			{
				i++;
				String[] listR = new String[this.rows.size() + 1];
				listR[0] = itemR.getKey();
				int num = 1;
				for (YuanRow r : this.getRows())
				{
					listR[num] = r.getItem(itemR.getKey()).getYuanColumnText();
					num++;
				}
				retureDic.put((short)i, listR);
			}
		}
		java.util.HashMap<String, String> tableInfo = new java.util.HashMap<String, String>();
		tableInfo.put("TableName", this.tableName);
		tableInfo.put("TableKey", this.tableKey);
		retureDic.put((short)0, tableInfo);
		retureDic.put((short)1, mRang);

		return retureDic;
	}



	/** 
	 从特定字典集合组合
	 
	 @param dic
	*/
	public final void CopyToDictionary(java.util.HashMap<Short, Object> dic)
	{
		if (dic.size() > 0)
		{


			this.Refresh();
			this.deleteRows.clear();
			this.Clear();




			for (java.util.Map.Entry<Short, Object> item : dic.entrySet())
			{

				if (item.getKey() == 0)
				{
					java.util.HashMap<String, String> tempDic = (java.util.HashMap<String, String>)item.getValue();
					this.tableName = tempDic.get("TableName");
					this.tableKey = tempDic.get("TableKey");
				}
				else
				{

					String[] listDc = (String[])item.getValue();
					int dcNum = 0;
					String dcStr = "";
					for (String dc : listDc)
					{
						if (dcNum == 0)
						{
							dcStr = dc;
						}
						else
						{
							if (this.rows.size() < dcNum)
							{
								YuanRow tempRow = new YuanRow();
								tempRow.Add(dcStr, dc);
								this.rows.add(tempRow);
							}
							else
							{
								this.rows.get(dcNum - 1).Add(dcStr, dc);
							}
						}
						dcNum++;
					}
				}
			}


		}
	}


	/** 
	 字典参数
	 
	*/
	public java.util.HashMap<Byte, Object> mParms;
	/** 
	 从特定字典集合组合表和字典参数
	 
	 @param dic
	*/
	public final void CopyToDictionaryAndParms(java.util.HashMap<Byte, Object> dic)
	{
		if (dic.size() > 0)
		{


			this.Refresh();
			this.deleteRows.clear();
			this.Clear();




			for (java.util.Map.Entry<Byte, Object> item : dic.entrySet())
			{

				if (item.getKey() == 0)
				{
					java.util.HashMap<String, String> tempDic = (java.util.HashMap<String, String>)item.getValue();
					this.tableName = tempDic.get("TableName");
					this.tableKey = tempDic.get("TableKey");
				}
				else if (item.getKey() == 1)
				{
					this.mParms = (java.util.HashMap<Byte, Object>)item.getValue();
				}
				else
				{

					String[] listDc = (String[])item.getValue();
					int dcNum = 0;
					String dcStr = "";
					for (String dc : listDc)
					{
						if (dcNum == 0)
						{
							dcStr = dc;
						}
						else
						{
							if (this.rows.size() < dcNum)
							{
								YuanRow tempRow = new YuanRow();
								tempRow.Add(dcStr, dc);
								this.rows.add(tempRow);
							}
							else
							{
								this.rows.get(dcNum - 1).Add(dcStr, dc);
							}
						}
						dcNum++;
					}
				}
			}


		}
	}

	/** 
	 获取字典参数
	 
	 @param dic
	 @return 
	*/
	public static java.util.HashMap<Short, Object> GetParms(java.util.HashMap<Short, Object> dic)
	{
		return (java.util.HashMap<Short, Object>)dic.get((short)1);
	}


	/** 
	 获取对应列的所有数据
	 
	 @param mColumnName 列名
	 @return 
	*/
	public final java.util.ArrayList<String> GetColumnText(String mColumnName)
	{
		java.util.ArrayList<String> getColumn = new java.util.ArrayList<String>();
		String strColumnText = "";
		for (YuanRow item : getRows())
		{
			RefObject<String> tempRef_strColumnText = new RefObject<String>(strColumnText);
			boolean tempVar = item.TryGetValue(mColumnName, tempRef_strColumnText);
				strColumnText = tempRef_strColumnText.argvalue;
			if (tempVar)
			{
				getColumn.add(strColumnText);
			}
		}
		return getColumn;
	}



}